package com.syf.starter.core.netty.tool;

import com.syf.core.service.SyServiceDefinition;
import com.syf.core.service.context.ServiceRequest;
import com.syf.core.service.context.ServiceResponse;
import io.netty.buffer.ByteBuf;

/**
 * 将netty通信协议中msg对象转换为service request
 */
public interface MsgCovert<T> {

    /**
     * 转换请求对象
     */
    default ServiceRequest convertRequest(SyServiceDefinition serviceDefinition, T requestMsg) throws Exception {
        ServiceRequest serviceRequest = buildServiceRequest(serviceDefinition, requestMsg);
        serviceRequest.getHead().setServiceDefinition(serviceDefinition);
        return serviceRequest;
    }

    /**
     * 创建service request
     */
    ServiceRequest buildServiceRequest(SyServiceDefinition serviceDefinition, T msg) throws Exception;

    /**
     * 转换响应对象
     */
    ByteBuf convertResponse(T requestMsg, ServiceResponse response) throws Exception;

}
